package com.yyt.thymeleafdemo.controller;

import com.yyt.thymeleafdemo.domain.User;
import com.yyt.thymeleafdemo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: yeyangtao
 * @date: 2019/12/24 20:45
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/")
    public String list(Model model){
        List<User> userList = userService.search();
        model.addAttribute(userList);
        return "user/list";
    }
    @PostMapping("doadd")
    public String add(User user){
        userService.create(user);
        return "redirect:/user/";
    }
    @GetMapping("add")
    public String add(){
        return "user/add";
    }

    @GetMapping("find-one")
    public String findOne(@RequestParam Long id,Model model){
        User user = userService.findById(id);
        model.addAttribute(user);
        return "user/update";
    }
    @RequestMapping("delete")
    public String delete(@RequestParam Long id){
        userService.deleteById(id);
        return "redirect:/user/";
    }
    @PostMapping("update")
    public String update(User user){
        userService.create(user);
        return "redirect:/user/";
    }
}
